package org.tmad.ocms.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

import org.apache.log4j.Logger;
import org.tmad.ocms.log.LogFactory;
import org.tmad.ocms.model.Volunteer;
import org.tmad.ocms.util.CommonUtils;

public class RegistrationDAO {

	@SuppressWarnings("unused")
	private static Logger logger = LogFactory.getLogger();
	Connection con = null;

	public boolean login(String username, String pwd) throws Exception {
		boolean result = false;

		con = ConnectionManager.getDbConnection();
		Statement stmt = con.createStatement();
		ResultSet results = stmt
				.executeQuery("select * from volunteer where username='" + username
						+ "' and password ='" + pwd + "'");	
		while (results.next()) {
			result = true;
		}
		return result;
	}

	public boolean register(Volunteer user) throws Exception {
		con = ConnectionManager.getDbConnection();
		PreparedStatement stmt = con
				.prepareStatement("insert into volunteer(email_id, password, first_name, last_name,dob) values(?,?,?,?,?)");
		stmt.setString(1, user.getEmailId());
		stmt.setString(2, CommonUtils.getEncryptedPassword(user.getPassword()));
		stmt.setString(3, user.getFirstName());
		stmt.setString(4, user.getLastName());
		stmt.setDate(5, CommonUtils.getDateFromString(user.getDob()));
		
				return stmt.execute();

	}

}
